﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

namespace NJL.Web
{
    public partial class CompanyOrderOK : BasePage
    {
        private readonly NJL.BLL.NJL_BOOK bllBook = new BLL.NJL_BOOK();
        private readonly NJL.BLL.NJL_BOOK_ORDER bllBookOrder = new BLL.NJL_BOOK_ORDER();
        private readonly NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();

        protected NJL.Model.NJL_BOOK modelBook;
        protected NJL.Model.NJL_BOOK_ORDER modelBookOrder;
        protected NJL.Model.NJL_COMPANY modelCompany;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }

        private void bind()
        {
            modelBookOrder = bllBookOrder.GetModel(Request.QueryString["id"]);
            litqpr.Text = modelBookOrder.PICK_NAME;
            ltilxfs.Text = modelBookOrder.PICK_MOBILE;
            litydr.Text = modelBookOrder.BOOK_NAME;
            litydrlxfs.Text = modelBookOrder.BOOK_MOBILE;
            litywrq.Text = string.Format("{0:yyyy-MM-dd}", modelBookOrder.VISIT_DATE);
            lttydzs.Text = modelBookOrder.TICKET_COUNT.ToString();
            litqpdd.Text = "景区售票处";
            litOrderNumber.Text = modelBookOrder.ORDER_NO;
            litdj.Text = string.Format("{0:00}", modelBookOrder.BOOK_PRICE);
            modelBook = bllBook.GetModel(modelBookOrder.NJL_BOOK_ID);
            if (modelBook != null)
            {
                //取票地点
                litqpdd.Text = modelBook.PICK_TICKET_ADDRESS;

                litdpjj.Text = string.Format("{0:0}", modelBook.BACK_PRICE);
                litxfje.Text = string.Format("{0:0}", modelBookOrder.ORDER_TOTAL);
                modelCompany = bllCompany.GetModel(modelBook.TARGET_ID);
                if (modelCompany != null)
                {
                    litName.Text = modelCompany.COMPANY_NAME;
                    litName1.Text = modelCompany.COMPANY_NAME;
                }
            }

            //如果有接收短信的手机号码，则发送给商户的短信接收号码
            string strMobileHost = System.Configuration.ConfigurationManager.AppSettings["CompanySmsMobile"];
            if (modelCompany.RECEIVE_SMS_MOBILE != null && modelCompany.RECEIVE_SMS_MOBILE != "")
            {
                strMobileHost = modelCompany.RECEIVE_SMS_MOBILE;
            }

            string strWebsiteName = WEBSITE_NAME;
            string strCompanyNameSms = modelCompany.COMPANY_NAME_SMS;
            string strPlayDate = modelBookOrder.VISIT_DATE.Value.ToString("MM月dd日");
            string strTitcketType = modelBook.BOOK_NAME;
            string strTitcketCount = modelBookOrder.TICKET_COUNT.Value.ToString();
            string strTotal = modelBookOrder.ORDER_TOTAL.Value.ToString("#");
            string strBookPrice = modelBookOrder.BOOK_PRICE.Value.ToString("#");
            string strOrderNo = modelBookOrder.ORDER_NO;
            string strPayType = modelBookOrder.TYPE_CODE == "2" ? "付款" : "直接";

            string strPickName = modelBookOrder.PICK_NAME;
            string strPickMobile = modelBookOrder.PICK_MOBILE;

            string strBookName = modelBookOrder.BOOK_NAME;
            string strBookMobile = modelBookOrder.BOOK_MOBILE;

            //取票地点
            string strPickTicketAddress = litqpdd.Text;

            string strContentToPickerTemplateSelf = GetSmsTemplate("C01");
            string strContentToPickerTemplateOther = GetSmsTemplate("C02");
            string strContentToCompanyTemplate = GetSmsTemplate("C03");

            string SMSContentToPicker;
            string SMSContentToCompany;

            //发给商户的短信
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$WebsiteName$}", strWebsiteName);
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$PickName$}", strPickName);
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$PickMobile$}", strPickMobile);
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$CompanyNameSms$}", strCompanyNameSms);
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$PlayDate$}", strPlayDate);
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$TitcketType$}", strTitcketType);
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$TitcketCount$}", strTitcketCount);
            //strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$Total$}", strTotal);
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$BookPrice$}", strBookPrice);//预定单价
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$OrderNo$}", strOrderNo);
            strContentToCompanyTemplate = strContentToCompanyTemplate.Replace("{$PayType$}", strPayType);
            SMSContentToCompany = strContentToCompanyTemplate;

            if (modelBookOrder.PICK_MOBILE != modelBookOrder.BOOK_MOBILE)
            {
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$BookName$}", strBookName);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$BookMobile$}", strBookMobile);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$CompanyNameSms$}", strCompanyNameSms);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$PlayDate$}", strPlayDate);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$TitcketType$}", strTitcketType);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$TitcketCount$}", strTitcketCount);
                //strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$Total$}", strTotal);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$BookPrice$}", strBookPrice);//预定单价
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$OrderNo$}", strOrderNo);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$PayType$}", strPayType);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$WebsiteName$}", strWebsiteName);
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$CompanyContactMobile$}", strMobileHost);
                //增加取票地点
                strContentToPickerTemplateOther = strContentToPickerTemplateOther.Replace("{$PickTicketAddress$}", strPickTicketAddress);
                SMSContentToPicker = strContentToPickerTemplateOther;
            }
            else
            {
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$CompanyNameSms$}", strCompanyNameSms);
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$PlayDate$}", strPlayDate);
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$TitcketType$}", strTitcketType);
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$TitcketCount$}", strTitcketCount);
                //strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$Total$}", strTotal);
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$BookPrice$}", strBookPrice);//预定单价
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$OrderNo$}", strOrderNo);
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$PayType$}", strPayType);
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$WebsiteName$}", strWebsiteName);
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$CompanyContactMobile$}", strMobileHost);
                //增加取票地点
                strContentToPickerTemplateSelf = strContentToPickerTemplateSelf.Replace("{$PickTicketAddress$}", strPickTicketAddress);
                SMSContentToPicker = strContentToPickerTemplateSelf;
            }

            if (modelBookOrder.PICK_MOBILE.Length == 11)
            {
                string i = base.SendSMS(modelBookOrder.PICK_MOBILE, SMSContentToPicker);

                i = base.SendSMS(strMobileHost, SMSContentToCompany);
                modelBookOrder.SEND_SMS_COUNT += 1;
                modelBookOrder.SEND_SMS_LAST_DATE_TIME = DateTime.Now;
                bllBookOrder.Update(modelBookOrder);
            }

            if (modelBookOrder.BOOK_EMAIL != null && modelBookOrder.BOOK_EMAIL != "")
            {
                //发送邮件
            }

        }

        private string GetSmsTemplate(string TEMPLATE_TYPE)
        {
            Encoding code = Encoding.GetEncoding("UTF-8");
            // 读取模板文件 
            string temp = "";
            if (TEMPLATE_TYPE == "C01")
            {
                temp = HttpContext.Current.Server.MapPath("~/SMSTemplate/SMS_Book_Company_01.txt");
            }
            if (TEMPLATE_TYPE == "C02")
            {
                temp = HttpContext.Current.Server.MapPath("~/SMSTemplate/SMS_Book_Company_02.txt");
            }
            if (TEMPLATE_TYPE == "C03")
            {
                temp = HttpContext.Current.Server.MapPath("~/SMSTemplate/SMS_Book_Company_03.txt");
            }
            StreamReader sr = null;
            //StreamWriter sw = null;
            string str = "";
            try
            {
                sr = new StreamReader(temp, code);
                str = sr.ReadToEnd(); // 读取文件 
            }
            catch (Exception exp)
            {
                HttpContext.Current.Response.Write(exp.Message);
                HttpContext.Current.Response.End();
                sr.Close();
            }
            return str;
        }
    }
}